package ro.unibuc.nautilus.controllers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import ro.unibuc.nautilus.domain.Harbour;
import ro.unibuc.nautilus.domain.Route;
import ro.unibuc.nautilus.domain.Ship;
import ro.unibuc.nautilus.service.ShipsService;

public class ShipsController extends MultiActionController {

	ShipsService shipsService;

	public ModelAndView ships(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView("ships");
		mav.addObject("ships", shipsService.findAll());
		mav.addObject("currentRoute", new ArrayList<Route>());
		return mav;
	}

	public ModelAndView shipRoute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		long shipId = ServletRequestUtils.getLongParameter(request, "shipId",
				-1);
		if (shipId == -1) {
			return null;
		}
		Ship ship = shipsService.findShip(shipId);

		// create JSONArray

		JSONArray route = new JSONArray();

		JSONObject harbour = new JSONObject();
		if (ship.getLastLocation() != null) {
			// add lastLocation as well

			harbour.put("lat", ship.getLastLocation().getLatitude());
			harbour.put("lon", ship.getLastLocation().getLongitude());
			harbour.put(
					"context",
					ship.getLastLocation().getName()
							+ " "
							+ new SimpleDateFormat("dd/MM/yyyy").format(ship
									.getLocationReportDate()));

		} else {
			harbour.put("context", "null");
		}
		route.add(harbour);
		
		if (ship.getCurrentRoute() != null) {
			for (Harbour h : ship.getCurrentRoute().getHarbours()) {
				harbour = new JSONObject();
				harbour.put("lat", h.getLatitude());
				harbour.put("lon", h.getLongitude());
				harbour.put("context", h.getName());
				route.add(harbour);
			}
		}
		// Prepare to send JSON
		response.setContentType("text/json; charset=utf-8");
		response.setCharacterEncoding("utf-8");
		response.getOutputStream().write(route.toString().getBytes());
		
		return null;
	
	}
	
	public ModelAndView getCurrentRoute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		long shipId = ServletRequestUtils.getLongParameter(request, "shipId",
				-1);
		if (shipId == -1) {
			return null;
		}
		Ship ship = shipsService.findShip(shipId);
		
		ArrayList<Route> cr = new ArrayList<Route>(1);
		
		if (ship.getCurrentRoute() != null) {
			cr.add(ship.getCurrentRoute());
		}
		ModelAndView mav = new ModelAndView("ships");
		mav.addObject("ships", shipsService.findAll());
		mav.addObject("currentRoute", cr);
		return mav;
	}

	@Required
	public void setShipsService(ShipsService shipsService) {
		this.shipsService = shipsService;
	}
}